package com.net.oil.car.adapter;
/*
 * Created by lch on 2018/2/26 0026.
 */

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.car.bean.CarBean;
import com.net.oil.http.AppUrl;
import com.net.oil.util.StringUtil;
import com.net.oil.util.glide.GlideUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyCarRecyclerViewAdapter extends RecyclerView.Adapter<MyCarRecyclerViewAdapter.ViewHolder> {


    private Context context;
    private LayoutInflater inflater;
    private List<CarBean> carBeans;
    private int checkPosition = 0;
    private RecyclerView recyclerView;

    public MyCarRecyclerViewAdapter(Context context, RecyclerView recyclerView) {
        this.context = context;
        this.recyclerView = recyclerView;
        inflater = LayoutInflater.from(context);
        carBeans = new ArrayList<>();
    }

    public void updateData(List<CarBean> carBeans){
        this.carBeans.clear();
        this.carBeans.addAll(carBeans);
        notifyDataSetChanged();
    }

    public void setCheckPosition(int checkPosition) {
        this.checkPosition = checkPosition;
    }

    /**
     * 返回选中的车的id
     * @return  id
     */
    public int getSelectCarId(){
        return  carBeans.get(checkPosition).getId();
    }

    /**
     * 返回选中的爱车的type
     * @return  type
     */
    public String getSelectCarInfo(){
        CarBean carBean = carBeans.get(checkPosition);
        if (carBean !=  null){
            CarBean.CarModelInfoBean carModelInfoBean = carBean.getCarModelInfo();
            if (carModelInfoBean != null){
                return carModelInfoBean.getType();
            }
        }
        return "";
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.my_car_recycler_view_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position, List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
        CarBean carBean = carBeans.get(position);
        holder.checkbox.setChecked(checkPosition == position);
        if (payloads.isEmpty()){
            if (carBean != null){
                CarBean.CarModelInfoBean carModelInfoBean = carBean.getCarModelInfo();
                if (carModelInfoBean != null){
                    if (!StringUtil.isEmpty(carModelInfoBean.getBrand())){
                        holder.carNameTv.setText(carModelInfoBean.getBrand());
                    }
                    if (!StringUtil.isEmpty(carModelInfoBean.getType())){
                        holder.carDetailTv.setText(carModelInfoBean.getType());
                    }
                    if (!StringUtil.isEmpty(carModelInfoBean.getBrandLogo())){
                        GlideUtil.getInstance().loadImage(context, holder.carLogo,
                                AppUrl.getImageUrl(carModelInfoBean.getBrandLogo()), R.drawable.car_logo_err);
                    }
                }
            }
            holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (!recyclerView.isComputingLayout() && recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_IDLE) {
                        notifyItemChanged(checkPosition, 0);
                    }
                    if (isChecked){
                        checkPosition = position;
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return carBeans == null ? 0 : carBeans.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.car_logo)
        ImageView carLogo;
        @BindView(R.id.car_name_tv)
        TextView carNameTv;
        @BindView(R.id.car_detail_tv)
        TextView carDetailTv;
        @BindView(R.id.checkbox)
        CheckBox checkbox;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
